/*	kd:
	
	In open-gl, your screen rotates nicely and you can do mostly what you know
	to be sane. It's all about making a rotation of your view and using what
	you know about right triangles.
	
	*/

class Gc_GlScreen : Gc_ProjScreen {
	
	protected vector3				forw_unit;
	protected vector3				right_unit;
	protected vector3				down_unit;
	
	override void Reorient (vector3 world_view_pos, vector3 world_ang) {
	
		// kd: Pitch is a weird gzd joke. It's probably to compensate looking
		// speed and all.
		world_ang.y = VectorAngle(
			cos(world_ang.y),
			sin(world_ang.y) * pixel_stretch);
		
		// kd: Regular schedule...
		super.Reorient(world_view_pos, world_ang);
		
		let cosang	= cos(world_ang.x);
		let cosvang	= cos(world_ang.y);
		let cosrang	= cos(world_ang.z);
		let sinang	= sin(world_ang.x);
		let sinvang	= sin(world_ang.y);
		let sinrang	= sin(world_ang.z);
		
		// kd: You learn this in preschool.
		let right_no_roll	= (
		-	sinang,
			cosang,
			0);
		
		let down_no_roll	= (
			sinvang * cosang,
			sinvang * sinang,
		-	cosvang);
		
		forw_unit = (
			cosvang * cosang,
			cosvang * sinang,
			sinvang);
		
		// kd: Pretend *_no_roll are x and y axes.
		down_unit	= cosrang * down_no_roll	+ sinrang * right_no_roll;
		right_unit	= sinrang * down_no_roll	- cosrang * right_no_roll;
	}
	
	// kd: Projection handling. These get called to make stuff a little faster,
	// since you may wanna project many many times.
	protected vector3				forw_in;
	protected vector3				right_in;
	protected vector3				down_in;
	
	override void BeginProjection () {
		forw_in		= forw_unit;
		right_in	= right_unit / tan_fov_2.x;
		down_in		= down_unit  / tan_fov_2.y;
		
		forw_in.z	*= pixel_stretch;
		right_in.z	*= pixel_stretch;
		down_in.z	*= pixel_stretch;
	}
	
	protected vector3				diff;
	
	override void ProjectWorldPos (vector3 world_pos) {
		diff		= levellocals.vec3diff(view_pos, world_pos);
		proj_pos	= (diff dot right_in, diff dot down_in);
		depth		= diff dot forw_in;
	}
	
	override vector2 ProjectToNormal () const {
		return proj_pos / depth;
	}
	
	override vector2 ProjectToScreen () const {
		let normal_pos = proj_pos / depth + (1, 1);
		
		return 0.5 * (
			normal_pos.x * resolution.x,
			normal_pos.y * resolution.y);
	}
	
	// kd: Same deal but backwards-ish.
	protected vector3				forw_out;
	protected vector3				right_out;
	protected vector3				down_out;
	
	override void BeginDeprojection () {
		
		// kd: Same deal as above, but reversed. This time, we're compensating
		// for what we rightfully assume is a projected position.
		
		// Look further.
		forw_out	= forw_unit;
		right_out	= right_unit * tan_fov_2.x;
		down_out	= down_unit  * tan_fov_2.y;
		
		forw_out.z	/= pixel_stretch;
		right_out.z /= pixel_stretch;
		down_out.z	/= pixel_stretch;
	}
	
	override vector3 DeprojectNormalToDiff (
	vector2	normal_pos,
	double	depth) const {
		
		// kd: It helps to plug in everything. It looks like this then:
		
		// depth * (
		//	forw_out +
		//	normal_pos.x * tan(hor_fov / 2) * right_out +
		//	normal_pos.y * tan(ver_fov / 2) * down_out)
		
		// where the *_out vectors are same as *_unit, but scaled on z-axis by
		// 1 / pixel_stretch. So yea... basically right triangles.
		
		// Check this by plugging the result into the projection formula above.
		
		return depth * (
			forw_out +
			normal_pos.x * right_out +
			normal_pos.y * down_out);
	}
	
	override vector3 DeprojectScreenToDiff (
	vector2	screen_pos,
	double	depth) const {
	
		// kd: Same thing...
		let normal_pos = 2 * (
			screen_pos.x / resolution.x,
			screen_pos.y / resolution.y) - (1, 1);
		
		return depth * (
			forw_out +
			normal_pos.x * right_out +
			normal_pos.y * down_out);
	}
}